# -*- encoding:utf-8 -*-
from flask import Flask, request, g
from psycopg2 import connect
from psycopg2.extras import RealDictCursor
from config import config
from lib import AppSession
from elasticsearch import Elasticsearch

from application.app_auth import app as auth_app
from application.app_group import app as group_app
from application.app_mechanism import app as mechanism_app
from application.app_information import app as information_app
from application.app_systemnode import app as systemnode_app
from application.app_operatelog import app as operatelog_app
from application.app_operation import app as operation_app
from application.app_search import app as search_app
from application.app_users import app as users_app
from application.app_safeportrain import app as safeportrain_app
from application.app_report import app as report_app
from application.app_analyze import app as analyze_app
from application.app_riskevents import app as riskevent_app
from application.app_riskwarning import app as riskwarning_app
from application.app_safetyform import app as safetyform_app
from application.app_event import app as event_app
from application.app_taskManagement import app as taskManagement_app


# 同步接口绑定
from synchronize.app_sync_role import app as sync_role_app
from synchronize.app_sync_user import app as sync_user_app
from synchronize.app_sync_mechanism import app as sync_mechanism_app
from synchronize.app_sync_information import app as sync_information_app
from synchronize.app_sync_systemnode import app as sync_systemnode_app
from synchronize.app_sync_riskwarning import app as sync_riskwarning_app

# 任务下发
from synchronize.app_task_assignment import app as task_assignment_app

app = Flask(__name__)
app.config.from_object(config.FlaskConfig)
app.register_blueprint(auth_app)
app.register_blueprint(group_app)
app.register_blueprint(mechanism_app)
app.register_blueprint(information_app)
app.register_blueprint(systemnode_app)
app.register_blueprint(operatelog_app)
app.register_blueprint(operation_app)
app.register_blueprint(search_app)
app.register_blueprint(users_app)
app.register_blueprint(safeportrain_app)
app.register_blueprint(taskManagement_app)
app.register_blueprint(report_app)
app.register_blueprint(analyze_app)
app.register_blueprint(event_app)

app.register_blueprint(riskevent_app)
app.register_blueprint(riskwarning_app)
app.register_blueprint(safetyform_app)
# 同步接口绑定
app.register_blueprint(sync_role_app)
app.register_blueprint(sync_user_app)
app.register_blueprint(sync_mechanism_app)
app.register_blueprint(sync_information_app)
app.register_blueprint(sync_systemnode_app)
app.register_blueprint(sync_riskwarning_app)


# 任务下发
app.register_blueprint(task_assignment_app)

app.session_interface = \
    AppSession.PostgresSessionInterface(connect(**config.DatabaseConfig.siem))


@app.before_request
def setupdb():
    # 供可视化使用的业务库连接
    g.conn = connect(**config.DatabaseConfig.siem)
    g.cursor = g.conn.cursor(cursor_factory=RealDictCursor)
    g.es = Elasticsearch(
        [config.ES_ADDR],
        http_auth=(config.ES_USER_NAME, config.ES_USER_PASSWD),
        port=config.ES_PORT,
        use_ssl=True,
        ca_certs=False,
        verify_certs=False,
        timeout=config.ES_TIMEOUT,
    )


@app.teardown_request
def unsetdb(exception):
    if g.cursor:
        g.cursor.close()
        g.cursor = None
    if g.conn:
        g.conn.close()
        g.conn = None


@app.errorhandler(404)
def err_notfound(ex):
    if request.is_xhr:
        return ('{"error":true, "msg":"请求的资源不存在"}', 404)
    else:
        return ("""<!DOCTYPE html>
                <html><head>
                    <meta charset="utf-8">
                    <title> 404  </title>
                </head><body>
                    <div style="text-align:center;
                                font-size:12em; color:#AAA;">404</div>
                    <hr style="width:80%;" />
                    <div style="text-align:center;
                                font-size:2em; color:#AAA;">
                                     Sorry!请求的资源不存在。</div>
                </body></html>""", 404)


@app.errorhandler(500)
def err_exception(ex):
    if request.is_xhr:
        return ('{"error":true, "msg":"服务器内部出现异常"}', 500)
    else:
        return ("""<!DOCTYPE html>
                <html><head>
                    <meta charset="utf-8">
                    <title> 500  </title>
                </head>
                <body>
                    <div style="text-align:center;
                                font-size:12em; color:#AAA;">500</div>
                    <hr style="width:80%;" />
                    <div style="text-align:center;
                                font-size:2em; color:#AAA;">
                                     Sorry!服务器内部出现异常</div>
                </body></html>""", 500)


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=9000, debug=True)
